package com.markyao.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Expiry;
import com.markyao.utils.SnowflakeIdGenerator;
import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Configuration
public class DefaultConfig {

    @Value("${server.username}")
    private String username;

    @Value("${server.password}")
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Bean
    public SnowflakeIdGenerator idGenerator(){
        return new SnowflakeIdGenerator(1,1);
    }

    @Bean("tokenCache")
    public Cache<String,String> tokenCache(){
        Cache<String, String> cache = Caffeine.newBuilder()
                .expireAfter(new Expiry<String, String>() {
                    @Override
                    public long expireAfterCreate(@NonNull String s, @NonNull String s2, long l) {
                        return l;
                    }

                    @Override
                    public long expireAfterUpdate(@NonNull String s, @NonNull String s2, long l, @NonNegative long l1) {
                        return l1;
                    }

                    @Override
                    public long expireAfterRead(@NonNull String s, @NonNull String s2, long l, @NonNegative long l1) {
                        return l1;
                    }
                })
                .initialCapacity(128)
                .build();
        return cache;
    }
}
